package com.tankwar.view.panel;

import com.tankwar.core.DrawAble;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

/**
 * 准备页面
 * 
 * @author lifeng
 * @date 2020年4月18日 下午4:18:56
 */
public class ReadyPanel extends AbstractPanel {

	/**
	 * 开始
	 */
	ReadyInfo info;
	ScenePanel scenePanel;

	public ReadyPanel(ScenePanel scenePanel, double width, double height) {
		super(width, height);
		this.scenePanel = scenePanel;
		this.info = new ReadyInfo();
		this.addDrawAble(info);
	}

	@Override
	public void start() {
		super.start();
		this.info.reset();
	}

	@Override
	public void stop() {
		super.stop();
	}

	/**
	 * 跳到下一页
	 */
	public void jump() {
		this.scenePanel.showGame();
	}

	/**
	 * 开始需要显示的文字和倒计时
	 * 
	 * @author lifeng
	 * @date 2020年4月18日 下午7:28:54
	 */
	public class ReadyInfo implements DrawAble {

		/**
		 * 需要显示的
		 */
		String ready = "准备中...";
		volatile int count = 3;
		Thread timer;

		/**
		 * 重置
		 */
		public void reset() {

			// 倒计时3
			count = 3;

			// 定时任务
			this.timer = new Thread(() -> {
				int step = 10;
				while (true) {
					step--;
					if (step == 0) {
						step = 10;
						count--;
					}
					if (count <= -1) {
						break;
					}
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						break;
					}
				}
			});
			this.timer.start();
		}

		@Override
		public void draw(GraphicsContext gc) {
			gc.setFill(Color.BLACK);
			gc.fillRect(0, 0, getWidth(), getHeight());
			gc.setFont(bigFont);
			gc.setFill(Color.GREEN);
			gc.fillText(ready, centerWidthOffset(-200), centerHeightOffset(-10));
			gc.fillText(count <= 0 ? "开始!" : String.valueOf(count), centerWidthOffset(100), centerHeightOffset(-10));
		}

		@Override
		public void update() {
			if (count <= -1) {
				jump();
			}
		}
	}
}
